package com.bingxue.edu.management.system.service;

import com.bingxue.edu.management.system.model.req.ResourceSortReq;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.system.model.entity.Resource;
import com.bingxue.edu.management.system.mapper.ResourceMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.bingxue.edu.management.system.model.entity.table.ResourceTableDef.RESOURCE;

/**
 * 资源业务实现
 *
 * @author panbo
 * @since 2024/07/21 22:36
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ResourceService extends BaseService<ResourceMapper, Resource> {

    /**
     * 排序资源
     */
    @Transactional(rollbackFor = Exception.class)
    public void sortResources(ResourceSortReq req) {
        int sort = 1;
        for (String dictItemId : req.getResourceIds()) {
            this.updateChain().where(RESOURCE.ID.eq(dictItemId)).set(RESOURCE.SORT, sort).update();
            sort ++;
        }
    }
}
